/*
 *
 * Copyright 2011, Ibrahim Arief
 * 
 * This file is part of Bambu Game Backend.
 *
 * Bambu Game Backend is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Bambu Game Backend is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Bambu Game Backend.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

package com.appspot.bambugame.server;

import java.util.Arrays;

import com.google.jplurk.ISettings;
import com.google.jplurk.PlurkClient;
import com.google.jplurk.exception.PlurkException;

public class PlurkService extends PlurkClient
{
    private static boolean mIsLoggedIn = false;
    
    private PlurkService( ISettings settings )
    {
        super( settings );
    }
    
    public PlurkService ensureLoggedIn()
    {
        if (!mIsLoggedIn)
        {
            try
            {
                this.login( PlurkServiceSettings.cUserID, PlurkServiceSettings.cUserPassword );
                mIsLoggedIn = true;
            }
            catch (PlurkException ex)
            {
                System.out.println("Plurk exception occurred when logging in : " + ex.getMessage() + ", " + Arrays.deepToString( ex.getStackTrace() ) );
            }
        }
        return this;
    }
    
    private static class SingletonHolder
    {
        public static final PlurkService instance = new PlurkService(PlurkServiceSettings.getSettings());
    }

    public static PlurkService getInstance()
    {
        return SingletonHolder.instance.ensureLoggedIn();
    }
}
